﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace LuxJQR
{
    public enum PresetColor
    {
        Yellow,
        Green,
        Red,
        White
    }

    /// <summary>
    /// 使用一个FastColoredTextBox <see cref="https://github.com/PavelTorgashov/FastColoredTextBox"/>
    /// 来输出信息，可以使用彩色文本了
    /// 这个控件不支持中文编码，有问题，先不用了
    /// </summary>
    public static class ColoredOutput
    {

       // private static ILogger _logger = LogManager.GetCurrentClassLogger();

        private static RichTextBox _coloredTextBox;
        //private static readonly TextStyle _higlightStyle = new TextStyle(Brushes.Yellow, null, FontStyle.Regular);
        //private static readonly TextStyle _infoStyle = new TextStyle(Brushes.Green, null, FontStyle.Regular);
        //private static readonly TextStyle _errorStyle = new TextStyle(Brushes.Red, null, FontStyle.Regular);
        //private static readonly TextStyle _defaultStyle = new TextStyle(Brushes.White, null, FontStyle.Regular);


        public static int MaxLines = 1000;

        public static void AttachOutput(RichTextBox textBox)
        {
            _coloredTextBox = textBox;
        }

        /// <summary>
        /// 正常颜色显示
        /// </summary>
        /// <param name="formatString"></param>
        /// <param name="args"></param>
        public static void WriteLine(string formatString, params object[] args)
        {
            var data = string.Format(formatString, args);

            //SaveLog(formatString);  //保存本地日志

            _coloredTextBox.BeginInvoke(new Action(() =>
            {
                AutoClear();

               // _coloredTextBox.AppendText(data + Environment.NewLine);

                _coloredTextBox.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss  ") + data + "\r\n");
                if (_coloredTextBox.Lines.Length > MaxLines)
                {
                    var rm = new List<int>();
                    for (int i = 0; i < MaxLines / 2; i++)
                    {
                        rm.Add(i);
                    }


                  
                }
                _coloredTextBox.ScrollToCaret();
            }));
        }

        public static void AutoClear()
        {
            int line = _coloredTextBox.TextLength;
            if (line >= 1024 * 10)
            {
                _coloredTextBox.Clear();
                line = 0;
            }
        }

        /// <summary>
        /// 可变颜色显示
        /// </summary>
        /// <param name="color">red, yellow, white, green</param>
        /// <param name="formatString"></param>
        /// <param name="args"></param>
        public static void WriteLine(PresetColor color, string formatString, params object[] args)
        {
            var data = string.Format(formatString, args);
            //SaveLog(formatString);

            Color style;

            switch (color)
            {
                case PresetColor.Yellow:
                    style = Color.Yellow;
                    break;
                case PresetColor.Green:
                    style = Color.Green;
                    break;
                case PresetColor.Red:
                    style = Color.Red;
                    break;
                case PresetColor.White:
                    style = Color.White;
                    break;
                default:
                    style = Color.White;
                    break;
            }

            _coloredTextBox.BeginInvoke(new Action(() =>
            {
                AutoClear();
                _coloredTextBox.SelectionColor = style;
               
                _coloredTextBox.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss  ") + data + "\r\n");
                _coloredTextBox.ScrollToCaret();
            }));
        }
        private static void SaveLog(string msg)
        {

            string logFileName = "";


            FileStream objFileStream = null;
            StreamWriter sw = null;

            try
            {

                string Filename = Param.LogFile + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\";
                if (!Directory.Exists(Filename))
                {
                    XmlHelper.CreateDirectoryEx(Filename);
                }
                logFileName=Filename+"Logs.csv";

                if (!File.Exists(logFileName))
                {
                    objFileStream = new FileStream(logFileName, FileMode.CreateNew, FileAccess.Write);
                }
                else
                {
                    objFileStream = new FileStream(logFileName, FileMode.Append, FileAccess.Write);
                }

                sw = new StreamWriter(objFileStream, System.Text.Encoding.GetEncoding(-0));


                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " , " + msg);
                sw.Close();
                objFileStream.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("请先关闭已打开的CSV文件,否则程序无法运行");
                sw.Close();
                objFileStream.Close();
            }

        }
    }
}
